package com.xy.tianyu.module.picture.fragment

import android.view.View
import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.module.picture.activity.PicSearchActivity
import com.xy.tianyu.module.picture.adapter.PicHotSearchAdapter
import com.xy.tianyu.module.picture.bean.SearchDayBean
import com.xy.tianyu.module.picture.bean.SearchDayItem
import com.xy.tianyu.network.Api
import kotlinx.android.synthetic.main.fragment_pic_hot_search.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import rxhttp.RxHttp
import rxhttp.async
import rxhttp.toClass

/**
 * @time 2020/9/30 9:50
 * @class 描述:
 */
class PicHotSearchFragment :LazyLoadBaseFragment(){
    private val category = mutableListOf("婚嫁","舞蹈","汽车","儿童","靓女","影视","健康","设计","运动","时尚","搞笑",
        "军事","家居","动物","美食","游戏","动漫","读书","音乐","绘画","清新","星座","教育","花卉","摄影","文艺")
    private var pageIndex = 0
    private var searchIndex = 0
    private val picHotSearchList = mutableListOf<SearchDayItem>()
    private lateinit var picHotSearchAdapter:PicHotSearchAdapter
    override fun setContentView(): Int {
        return R.layout.fragment_pic_hot_search
    }

    override fun lazyLoad() {
        ry_pic_hot_search.layoutManager = GridLayoutManager(activity,2)
        picHotSearchAdapter = PicHotSearchAdapter(R.layout.item_pic_hot_search,picHotSearchList)
        ry_pic_hot_search.adapter = picHotSearchAdapter

        picHotSearchAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
                PicSearchActivity.actionStart(activity!!,picHotSearchList[position].search_word)
            }

        })

        getData(true)

        refresh_pic_hot_search.setOnRefreshListener {
            pageIndex = 0
            getData(true)
        }

        refresh_pic_hot_search.setOnLoadMoreListener {
            pageIndex++
            getData(false)
        }
    }

    override fun stopLoad() {

    }
    private suspend fun getSearchDay(): SearchDayBean {
        return RxHttp.get(Api.DAY_DAY_SEARCH_PICTURE_BASE_URL+"/search-day/$searchIndex")
            .toClass<SearchDayBean>()
            .await()
    }

    private suspend fun getHotSearchData(scope: CoroutineScope):Deferred<SearchDayBean>{
        return RxHttp.get(Api.DAY_DAY_SEARCH_PICTURE_BASE_URL+"/categories/${category.random()}/$pageIndex")
            .toClass<SearchDayBean>()
            .async(scope)
    }

    private fun getData(isRefresh:Boolean){
        rxLifeScope.launch({
            if (isRefresh){
                picHotSearchList.clear()
                picHotSearchAdapter.notifyDataSetChanged()
            }

            for (i in 0..2){
                val picHotSync = getHotSearchData(this)
                val searchDayBean = picHotSync.await()
                val list = searchDayBean.items
                if (list.isNotEmpty()){
                    picHotSearchList.addAll(list)
                }
            }
            picHotSearchAdapter.notifyDataSetChanged()
        },{
            ToastUtils.showShort(it.message)
        },{

        },{
            refresh_pic_hot_search.finishRefresh()
            refresh_pic_hot_search.finishLoadMore()
        })
    }
}